package com.szq.pattern.event;

import com.szq.pattern.LotteryResult;
import com.szq.pattern.event.listener.EventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: szq
 * @date: 2021-09-13 11:56:28
 */
public class EventManager {

    Map<Enum<EventType>, List<EventListener>> listeners = new HashMap<>();

    public enum EventType {
        MQ, Message
    }

    public EventManager(Enum<EventType>... operations) {
        for (Enum<EventType> operation : operations) {
            this.listeners.put(operation, new ArrayList<>());
        }
    }

    /**
     * 订阅
     * @param eventType
     * @param eventListener
     */
    public void subscribe(Enum<EventType> eventType, EventListener eventListener){
        List<EventListener> eventListeners = listeners.get(eventType);
        eventListeners.add(eventListener);
    }

    /**
     * 取消订阅
     * @param eventType
     * @param eventListener
     */
    public void unsubscribe(Enum<EventType> eventType, EventListener eventListener){
        List<EventListener> eventListeners = listeners.get(eventType);
        eventListeners.remove(eventListener);
    }

    /**
     * 通知
     * @param eventType
     * @param lotteryResult
     */
    public void notify(Enum<EventType> eventType, LotteryResult lotteryResult){
        List<EventListener> eventListeners = listeners.get(eventType);
        eventListeners.stream().forEach(t->{
            t.doEvent(lotteryResult);
        });
    }
}
